# WSD测试服务启动脚本
# 在新窗口中运行，可以实时查看控制台输出

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "WSD测试服务启动脚本" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# 检查防火墙规则
Write-Host "检查防火墙规则..." -ForegroundColor Yellow
$udpRule = Get-NetFirewallRule -DisplayName "WSD Test Service - Discovery" -ErrorAction SilentlyContinue
$tcpRule = Get-NetFirewallRule -DisplayName "WSD Test Service - Metadata" -ErrorAction SilentlyContinue

if (-not $udpRule) {
    Write-Host "  添加UDP 3702防火墙规则..." -ForegroundColor Yellow
    netsh advfirewall firewall add rule name="WSD Test Service - Discovery" dir=in action=allow protocol=UDP localport=3702 | Out-Null
    Write-Host "  ✓ UDP 3702规则已添加" -ForegroundColor Green
} else {
    Write-Host "  ✓ UDP 3702规则已存在" -ForegroundColor Green
}

if (-not $tcpRule) {
    Write-Host "  添加TCP 8080防火墙规则..." -ForegroundColor Yellow
    netsh advfirewall firewall add rule name="WSD Test Service - Metadata" dir=in action=allow protocol=TCP localport=8080 | Out-Null
    Write-Host "  ✓ TCP 8080规则已添加" -ForegroundColor Green
} else {
    Write-Host "  ✓ TCP 8080规则已存在" -ForegroundColor Green
}

Write-Host ""

# 获取程序路径
$exePath = Join-Path $PSScriptRoot "bin\Release\net462\WsdTestService.exe"

if (-not (Test-Path $exePath)) {
    Write-Host "错误: 找不到程序文件: $exePath" -ForegroundColor Red
    Write-Host "请先编译项目: dotnet build -c Release" -ForegroundColor Yellow
    exit 1
}

# 检查是否已经有实例在运行
$existing = Get-Process -Name WsdTestService -ErrorAction SilentlyContinue
if ($existing) {
    Write-Host "检测到已有WSD测试服务在运行（进程ID: $($existing.Id)）" -ForegroundColor Yellow
    Write-Host "是否要停止旧进程并启动新的？(Y/N)" -ForegroundColor Yellow
    $answer = Read-Host
    if ($answer -eq "Y" -or $answer -eq "y") {
        Stop-Process -Id $existing.Id -Force -ErrorAction SilentlyContinue
        Start-Sleep -Seconds 1
        Write-Host "旧进程已停止" -ForegroundColor Green
    } else {
        Write-Host "退出" -ForegroundColor Yellow
        exit 0
    }
}

Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "正在启动WSD测试服务..." -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "提示:" -ForegroundColor Yellow
Write-Host "  - 程序需要在控制台窗口中运行以查看实时日志" -ForegroundColor Yellow
Write-Host "  - 按 Ctrl+C 停止服务" -ForegroundColor Yellow
Write-Host "  - 在另一台Windows电脑上打开'添加设备'测试" -ForegroundColor Yellow
Write-Host ""
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# 在新窗口中启动程序
$scriptPath = Join-Path $PSScriptRoot "bin\Release\net462"
Start-Process -FilePath $exePath -WorkingDirectory $scriptPath -NoNewWindow:$false

Write-Host "程序已在独立窗口中启动" -ForegroundColor Green
Write-Host "请在程序窗口中查看实时日志" -ForegroundColor Green
Write-Host ""
Write-Host "等待5秒后检查程序状态..." -ForegroundColor Yellow
Start-Sleep -Seconds 5

$proc = Get-Process -Name WsdTestService -ErrorAction SilentlyContinue
if ($proc) {
    Write-Host ""
    Write-Host "✓ 程序正在运行（进程ID: $($proc.Id)）" -ForegroundColor Green
    Write-Host ""
    Write-Host "请在另一台Windows电脑上测试能否发现打印机！" -ForegroundColor Cyan
} else {
    Write-Host ""
    Write-Host "✗ 程序可能启动失败，请检查程序窗口中的错误信息" -ForegroundColor Red
}

